package cn.z2huo.knowledge.concurrency.volatilekeyword;

/**
 * <p>指令重排序 测试
 *
 * @author z2huo
 */
class InstructionRecorderTest {

    private volatile static int x = 0, y = 0;

    private static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        for (;;) {
            i++;

            x = 0;
            y = 0;
            a = 0;
            b = 0;

            Thread t1 = new Thread(() -> {
                a = 1;
                x = b;
            });
            Thread t2 = new Thread(() -> {
                b = 1;
                y = a;
            });
            t1.start();
            t2.start();
            t1.join();
            t2.join();

            String result = "No." + i + " (" + x + ", " + y + ")";
            if (x == 0 && y == 0) {
                System.out.println(result);
            }
        }
    }
}
/*
No.9095 (0, 0)
No.123745 (0, 0)
No.134276 (0, 0)

 */